package com.emdata.competition.service.impl;

import com.emdata.competition.common.FileConfig;
import com.emdata.competition.common.ResultData;
import com.emdata.competition.common.page.MyPage;
import com.emdata.competition.common.util.EmdataFileUtils;
import com.emdata.competition.common.util.LocalCacheClient;
import com.emdata.competition.controller.vo.QueryParam;
import com.emdata.competition.service.IFileService;
import com.emdata.competition.service.bo.PicBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @description: 文件操作服务实现类
 * @date: 2020/12/15
 * @author: sunming
 */
@Service
public class FileServiceImpl implements IFileService {

    @Autowired
    private FileConfig fileConfig;

    /**
     * 查询图片
     *
     * @param queryParam 查询参数
     * @return 分页查询结果
     */
    @Override
    public MyPage<PicBO> listFile(QueryParam queryParam) {
        String fileName = queryParam.getFileName();
        // 从缓存中取
        Map<String, PicBO> cacheMap = LocalCacheClient.cacheMap;
        List<PicBO> picBOS = new ArrayList<>();
        for (String key : cacheMap.keySet()) {
            String name = key.substring(key.lastIndexOf(EmdataFileUtils.PATH_SEPERIATOR_LINUX) + 1);
            if (name.contains(fileName)) {
                picBOS.add(cacheMap.get(key));
            }
        }
        MyPage<PicBO> boMyPage = new MyPage<>(queryParam.getPageNum(), queryParam.getPageSize()
                , picBOS.size(), picBOS);
        return boMyPage;
    }

    /**
     * 逻辑删除文件
     *
     * @param fileName 文件名
     * @return 是否删除成功
     */
    @Override
    public ResultData deleteFile(String fileName) {
        String competitionFilePath = fileConfig.getCompetitionFilePath();
        String competitionFileDeletePath = fileConfig.getCompetitionFileDeletePath();
        Map<String, PicBO> cacheMap = LocalCacheClient.cacheMap;
        String filePath = competitionFilePath + "/" + fileName;
        File file = new File(filePath);
        // 判断要删除的文件是否存在
        if (file.exists()) {
            String newFilePath = competitionFileDeletePath + "/" + file.getName();
            boolean b = file.renameTo(new File(newFilePath));
            if (b) {
                cacheMap.remove(file.getPath());
                return ResultData.success();
            } else {
                return ResultData.error();
            }
        } else {
            return ResultData.error("文件不存在");
        }
    }

    /**
     * 初始化
     */
    @Override
    public void init() {
        String competitionFilePath = fileConfig.getCompetitionFilePath();
//        String baseUrl = fileConfig.getCompetitionFileBaseUrl();
        List<File> files = EmdataFileUtils.listFile(competitionFilePath, true);
        for (File file : files) {
            PicBO picBO = new PicBO();
            picBO.setName(file.getName());
            picBO.setPicUrl(file.getPath());
            picBO.setTime(file.lastModified());
            picBO.setSize(file.length());
            LocalCacheClient.cacheMap.put(file.getPath(), picBO);
        }
    }

}
